package com.wits.qso.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.wits.platform.service.BaseService;
import com.wits.platform.service.account.ShiroUser;
import com.wits.qso.common.enumerate.GroupStatus;
import com.wits.qso.common.enumerate.HelpQueStatus;
import com.wits.qso.common.enumerate.MemberStatus;
import com.wits.qso.dao.DiscussionGroupDao;
import com.wits.qso.dao.DiscussionMemberDao;
import com.wits.qso.dao.HelpQueDao;
import com.wits.qso.entity.DiscussionGroup;
import com.wits.qso.entity.DiscussionMember;
import com.wits.qso.entity.HelpQue;

@Component
public class DiscussionGroupService extends BaseService<DiscussionGroup, String>{


	private DiscussionGroupDao discussionGroupDao;
	
	@Resource
	private DiscussionMemberDao discussionMemberDao;
	
	@Resource
	private HelpQueDao helpQueDao;

	@Resource
	public void setDiscussionGroupDao(DiscussionGroupDao discussionGroupDao) {
		this.baseDao=discussionGroupDao;
		this.discussionGroupDao = discussionGroupDao;
	}
	
	/**
	 * 
	 * 同时保存讨论组成员,并设置问题状态为讨论
	 * @param entity
	 * @param memberIds 讨论组成员id集合
	 * @param hpqIdArr  问题id数组
	 * @throws Exception 
	 */
	public void saveGroupAndUpdateQuestion(ShiroUser user,DiscussionGroup entity,List<String> memberIds,List<String> hpqIdList) throws Exception{
		discussionGroupDao.save(entity);
		DiscussionMember member=null;
		List<DiscussionMember> list=new ArrayList<DiscussionMember>();
		for(String userId : memberIds){
			member=new DiscussionMember(entity.getId(),userId,MemberStatus.online.value());
			list.add(member);
		}
		Map<String,Object> value=new HashMap<String,Object>();
		value.put("hpqStatus", HelpQueStatus.DC.getValue());
		value.put("teaId", user.getId());
		value.put("teaName", user.getName());
		value.put("groupId", entity.getId());
		for(String id : hpqIdList){
			helpQueDao.updateField(HelpQue.class, id,value);
		}
		discussionMemberDao.save(list);
	}

	public void addMember(String groupId,String userId) {
		DiscussionMember member=new DiscussionMember(groupId,userId,MemberStatus.online.value());
		discussionMemberDao.save(member);
	}
	
	public void updateMembers(String groupId, List<String> memberIds,List<String> deSelectedList) {
		DiscussionMember member=null;
		if(deSelectedList!=null && deSelectedList.size()>0){
			discussionMemberDao.executeUpdate("update DiscussionMember set status=?1 where id.groupId=?2 and id.userId in(?3)",MemberStatus.deleted.value(),groupId,deSelectedList);
		}
		List<DiscussionMember> list=new ArrayList<DiscussionMember>();
		if(memberIds!=null && memberIds.size()>0){
			for(String userId : memberIds){
				member=new DiscussionMember(groupId,userId,MemberStatus.online.value());
				list.add(member);
			}
		}
		discussionMemberDao.save(list);
	}
	
	
	/**
	 * 
	 * 更新讨论组状态及问题状态
	 * @param groupId
	 * @param gs
	 * @param hpqIdArray
	 * @param queStatus
	 * @throws Exception
	 */
	public void updateGroupStatusAndQuestionStatus(String groupId,GroupStatus gs,String[] hpqIdArray,HelpQueStatus queStatus) throws Exception{
		if(hpqIdArray!=null && hpqIdArray.length>0){
			for(String id : hpqIdArray){
				helpQueDao.updateField(HelpQue.class, id,"hpqStatus", queStatus.getValue());
			}
		}
		discussionGroupDao.updateField(DiscussionGroup.class, groupId, "status",gs.value());
	}
	
	

}
